package com.ask.service.Impl;

import com.ask.constant.StatusConstant;
import com.ask.entity.Dish;
import com.ask.entity.Orders;
import com.ask.mapper.DishMapper;
import com.ask.mapper.OrderMapper;
import com.ask.mapper.SetMealMapper;
import com.ask.mapper.UserMapper;
import com.ask.service.OrderService;
import com.ask.service.ReportService;
import com.ask.service.WorkSpaceService;
import com.ask.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setmealMapper;


    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getDayBusinessData(LocalDateTime begin, LocalDateTime end) {

        // 构造当日时间段
        Map map = new HashMap();
        map.put("beginTime", begin);
        map.put("endTime", end);

        // 查询当日的新增用户
        Integer newUsers = userMapper.countByMap(map);

        // 查询当日总的订单数量
        Integer totalOrderCount = orderMapper.countByMap(map);

        // 查询当日的有效订单数量
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(map);

        // 查询当日营业额turnover
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        // 计算当天的平均客单价
        Double unitPrice = 0.0;
        if (validOrderCount != 0)
            unitPrice = turnover / validOrderCount;

        // 计算当天的订单完成率
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0)
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        Map map = new HashMap();
        // 查询已经起售的菜品数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        // 查询停售的菜品
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return new DishOverViewVO(discontinued, sold);
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeal() {
        Map map = new HashMap();
        // 查询已经起售的套餐数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        // 查询停售的套餐
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return new SetmealOverViewVO(discontinued, sold);
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        Map map = new HashMap();
        map.put("beginTime", LocalDateTime.now().with(LocalTime.MIN));

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

}
